package com.zhz.mall.admin.modules.pms.controller;


import com.zhz.mall.admin.modules.pms.model.PmsProductCategory;
import com.zhz.mall.common.api.CommonPage;
import com.zhz.mall.common.api.CommonResult;
import com.zhz.mall.admin.dto.PmsProductCategoryDTO;
import com.zhz.mall.admin.dto.ProductCateChildrenDTO;
import com.zhz.mall.admin.modules.pms.service.PmsProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 产品分类 前端控制器
 * </p>
 *
 * @author zhouhengzhe
 * @since 2023-01-09
 */
@RestController
@RequestMapping("/productCategory")
public class PmsProductCategoryController {

    @Autowired
    PmsProductCategoryService productCategoryService;

    /**
     *  url:'/productCategory/list/'+parentId,
     *     method:'get',
     *     params:     {
     *           pageNum: 1,
     *           pageSize: 5
     *         },
     */
    @GetMapping("/list/{parentId}")
    public CommonResult<CommonPage<PmsProductCategory>> getList(@PathVariable Long parentId,
                                                                @RequestParam(value="pageNum",defaultValue = "1") Integer pageNum,
                                                                @RequestParam(value="pageSize",defaultValue = "5") Integer pageSize){
        return CommonResult.success(CommonPage.restPage(productCategoryService.list(parentId,pageNum,pageSize)));
    }



    /**
     *  修改导航栏显示状态
     *  url: /productCategory/update/navStatus
     *  method:'post',
     *  data:data
     *      data.append('ids',ids);
     *      data.append('navStatus',row.navStatus);
     */
    @PostMapping("/update/navStatus")
    public CommonResult updateNavStatus(@RequestParam(value="ids") List<Long> ids,
                                        @RequestParam(value="navStatus") Integer navStatus){
        return productCategoryService.updateNavStatus(ids, navStatus)? CommonResult.success(true) : CommonResult.failed();
    }

    /**
     * 商品分类删除
     *  url:'/productCategory/delete/'+id,
     *     method:'post'
     */
    @PostMapping("/delete/{id}")
    public CommonResult<Boolean> delete(@PathVariable Long id){
        return productCategoryService.removeById(id)? CommonResult.success(true) : CommonResult.failed();
    }

    /**
     * 分类添加
     * url:'/productCategory/create',
     *     method:'post',
     *     data:data
     */
    @PostMapping("/create")
    public CommonResult<Boolean> create(@RequestBody PmsProductCategoryDTO productCategoryDTO){
        return productCategoryService.customSave(productCategoryDTO)? CommonResult.success(true) : CommonResult.failed();
    }


    /**
     *  根据id获取商品分类
     *  return request({
     *     url:'/productCategory/'+id,
     *     method:'get',
     *   })
     */
    @GetMapping("/{id}")
    public CommonResult<PmsProductCategory> getById(@PathVariable Long id){
        return CommonResult.success(productCategoryService.getById(id));
    }

    /**
     * 修改产品分类
     *   url:'/productCategory/update/'+id,
     *     method:'post',
     *     data:data
     *
     */
    @PostMapping("/update/{id}")
    public CommonResult<Boolean> update(
            @RequestBody PmsProductCategoryDTO productCategoryDTO){
        return productCategoryService.update(productCategoryDTO)? CommonResult.success(true) : CommonResult.failed();
    }

    /**
     *   获取商品一级分类和二级分类的下拉级联数据
     *   url:'/productCategory/list/withChildren',
     *     method:'get'
     */
    @GetMapping("/list/withChildren")
    public  CommonResult<List<ProductCateChildrenDTO>> getWithChildren(){
        return CommonResult.success(productCategoryService.getWithChildren());
    }
}

